package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.mongodb.client.result.UpdateResult;
import com.tanhua.model.mongo.SoulRecommend;
import com.tanhua.model.mongo.SoulResult;
import com.tanhua.model.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.core.query.UpdateDefinition;

import java.util.List;

@DubboService
public class SoulResultApiImpl implements SoulResultApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据ID查询灵魂测试结果
     *
     * @param id
     * @return
     */
    @Override
    public SoulResult findById(ObjectId id) {
        SoulResult soulResult = mongoTemplate.findById(id, SoulResult.class);
        return soulResult;
    }

    /**
     * 查询分数值相近的用户
     *
     * @return
     */
    @Override
    public List<Long> findByScore(Integer score, Long userId) {
        //大于方法
        Criteria gt = Criteria.where("score").gt(score-10);
        //小于方法
        Criteria lt = Criteria.where("score").lt(score+10);
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").nin(userId));
        query.addCriteria(new Criteria().andOperator(gt,lt)).limit(10).with(Sort.by(Sort.Order.desc("data")));
       // List<SoulResult> soulResults = mongoTemplate.find(query, SoulResult.class);

        String field = "userId";
        List<Long> ids = mongoTemplate.findDistinct(query, field, SoulResult.class,Long.class);
       // List<Long> ids = CollUtil.getFieldValues(soulResults, "userId", Long.class);
        return ids;
    }

    // 根据报告单ID查询用户报告单
    @Override
    public SoulResult findByReportId(String s) {

        Criteria criteria = Criteria.where("reportId").is(s);
        Query query = Query.query(criteria);
        return mongoTemplate.findOne(query, SoulResult.class);
    }

    @Override
    public ObjectId save(SoulResult soulResultInfo) {
        SoulResult save = mongoTemplate.save(soulResultInfo);
        SoulRecommend soulRecommend = new SoulRecommend();
        soulRecommend.setUserId(soulResultInfo.getUserId());
        soulRecommend.setLevel(soulRecommend.getLevel());
        soulRecommend.setAverageScore(soulResultInfo.getScore());
        soulRecommend.setRecommendStar(2);
        mongoTemplate.save(soulRecommend);
        return save.getId();
    }

    @Override
    public ObjectId updata(SoulResult soulResultInfo) {
        Criteria criteria = Criteria.where("_id").is(soulResultInfo.getId());
        //3、如果存在，更新
        Update update = Update.update("level",soulResultInfo.getLevel())
                .set("conclusion", soulResultInfo.getConclusion())
                .set("cover", soulResultInfo.getCover())
                        .set("score",soulResultInfo.getScore());
        mongoTemplate.updateFirst(Query.query(criteria), update, SoulResult.class);
        return soulResultInfo.getId();

    }
}
